<?php
/**
 * Representa un objeto de seguridad, utilizado para
 * crear consultas SQL seguras. Consiste en un objeto
 * que tendrá una propiedad variable, y un valor de
 * referencia que será comprobado para verificar la
 * integridad del token.
 *
 * @author Matias Mirabelli <project@moyrax.com>
 *
 * @package Kingorm
 */
class Token
{
	const PROPERTY = 0;
	const METHOD = 1;

	public    $name          = "";
	protected $relatedObject = null;
	protected $compareValue  = "";
	protected $compareData   = "";
	protected $isOk          = false;
	protected $type          = 0;

	public function __construct(&$relatedObject, $compareData)
	{
		$this->relatedObject = $relatedObject;
		$this->compareData   = $compareData;
	}

	final public function bind($propertyName, $type = PROPERTY)
	{
		if ( $type == PROPERTY )
			$type = self::PROPERTY;

		$this->compareValue = $propertyName;
		$this->type = $type;
	}

	final public function isVerified()
	{
		return( $this->isOk );
	}

	/**
	 * Verifica el estado del token. Si es correcto devuelve
	 * true, de lo contrario devuelve false.
	 */
	final public function assert()
	{
		if ( $this->type == self::PROPERTY ) {
			return( $this->assertProp() );
		}
		else {
			return( $this->assertMethod() );
		}

	}

	private function assertProp()
	{
		foreach ( $this->relatedObject as $key => $value )
		{
			if ( $key == $this->compareValue )
			{
				if ( $value != $this->compareData ) {
					$this->isOk = false;

					return(false);
				}

				$this->isOk = true;

				return(true);
			}
		}
	}

	private function assertMethod()
	{
		$procName = $this->compareValue;

		if ( $this->relatedObject->$procName() != $this->compareData )
		{
			$this->isOk = false;

			return(false);
		}

		$this->isOk = true;

		return(true);
	}
}
?>
